$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var cmdObj = {
        reload: function () {
            client_datagrid.datagrid("reload");
        },
        load: function () {
            $.messager.confirm('确认', '您确认要更新正式客户数据吗？', function (r) {
                if (r) {
                    $.get("/clientfollowup/load", function (data) {
                        if (data.success) {
                            //刷新列表数据
                            client_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            if(row){
                row["client.name"]=row.client.name
            }
            //添加与编辑时将数据清空
            client_form.form("clear");
            //数据回显: 同名原则
            client_form.form("load", row);
            //1.弹框
            client_dialog.dialog("open");
            //设置标题
            client_dialog.dialog("setTitle", "客户信息编辑");
        },
        //保存操作
        save: function () {
            var id = $("#clientId").val();
            url = "/clientfollowup/update";
            client_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        client_dialog.dialog("close");
                        //刷新列表数据
                        client_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            client_dialog.dialog("close");
        }
    }
    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    client_datagrid.datagrid({
        url: "clientfollowup/query",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        modal:true,
        toolbar: '#tb',
        columns: [[
            {field: "ck", checkbox: true},//显示多选框样式
            {
                field: "id", hidden: true,formatter: function (value, row, index) {
                    if(row.id){
                        return row.id;
                    }else{
                        return "获取客户姓名失败";
                    }
                }
             },

            {
                field: "name", title: "客户姓名", width: 100, formatter: function (value, row, index) {
                    if(row.client){
                        return row.client.name;
                    }else{
                        return "获取客户姓名失败";
                    }
                }
            },
            {
                field: "gender", title: "性别", width: 100, formatter: function (value, row, index) {
                if (row.client.gender == 1) {
                    return "男";
                } else if (row.client.gender == 0) {
                    return "女";
                }else{
                    return "未填写性别";
                }
            }
            },
            {
                field: "email", title: "邮箱", width: 100, formatter: function (value, row, index) {
                    if(row.client){
                        return row.client.email;
                    }else{
                        return "获取邮箱失败";
                    }
                }
            },
            {
                field: "tel", title: "客户电话", width: 100, formatter: function (value, row, index) {
                    if(row.client){
                        return row.client.tel;
                    }else{
                        return "获取客户电话失败";
                    }
                }
            },
            {
                field: "weixin", title: "微信", width: 100, formatter: function (value, row, index) {
                    if(row.client){
                        return row.client.weixin;
                    }else{
                        return "获取微信失败";
                    }
                }
            },
            {
                field: "vdate", title: "成为正式客户时间", width: 100, formatter: function (value, row, index) {
                    if(row){
                        return row.vdate;
                    }else{
                        return "获取成为正式客户时间失败";
                    }
                }
            },
            {
                field: "number", title: "总投保数量", width: 100, formatter: function (value, row, index) {
                if(row){
                    return row.number;
                }else{
                    return "获取成为正式客户时间失败";
                }
            }
            },
            {
                field: "totalAmont", title: "总投险金额", width: 100, formatter: function (value, row, index) {
                    if(row){
                        return row.totalAmont;
                    }else{
                        return "获取总投险金额失败";
                    }
                }
            },
            {
                field: "auditDate", title: "保单终止时间", width: 100, formatter: function (value, row, index) {
                    if(row){
                        return row.auditDate;
                    }else{
                        return "获取保单终止时间失败";
                    }
                }
            },
            {
                field: "state", title: "是否理赔过", width: 100, formatter: function (value, row, index) {
                    if(row.state==6){
                        return "已经理赔";
                    }else{
                        return "尚未理赔过";
                    }
                }
            },
            {field: "accident", title: "客户回馈", width: 100},
            {field: "planTwo", title: "二次开发计划", width: 100},
            {field: "remark", title: "备注", width: 100}
        ]],
    });
    client_dialog.dialog({
        width: 400,
        height: 300,
        buttons: "#bb",
        closed: true,
        resizable:true,
        draggable:true
    })
})


//工具栏的高级查询
function query() {

    /*关键字查询*/
    var ls = $("#leadingSreach").textbox("getValue");
    /*负责人任查询*/
    var kw = $("#keyword").textbox("getValue");

    /*根据创建时间查询*/
    /*开始时间*/
    var bd = $("#beginDate").textbox("getValue");
    /*结束时间*/
    var ed = $("#endDate").textbox("getValue");
    $("#client_datagrid").datagrid("load", {
        //leadingSreach: ls,
        //beginDate: bd,
        //endDate: ed,
        keyword: kw
    });
}